package com.future.hair.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.future.hair.server.config.jjwt.JwtHelper;
import com.future.hair.server.config.result.CommonResult;
import com.future.hair.server.entity.Category;
import com.future.hair.server.entity.ReserveCategory;
import com.future.hair.server.entity.UserInfo;
import com.future.hair.server.entity.UserOrder;
import com.future.hair.server.mapper.ReserveCategoryMapper;
import com.future.hair.server.mapper.UserInfoMapper;
import com.future.hair.server.mapper.UserOrderMapper;
import com.future.hair.server.model.PageModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;



/**
*
* @author future-RogueYBJ
* @since 2020-05-23
* @version v1.0
*/

@RestController
@RequestMapping("//hair/userOrder")
@Api(tags = "UserOrder管理接口")

public class UserOrderController{
    @Autowired
    private UserOrderMapper mapper;

    @Autowired
    private UserInfoMapper infoMapper;

    @Autowired
    private ReserveCategoryMapper reserveCategoryMapper;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @ApiOperation(value = "查询 UserOrder 列表")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public CommonResult<List<UserOrder>> userOrderList(@RequestBody PageModel data,@RequestHeader String token) {
        return CommonResult.data(mapper.selectPage(new Page<>(data.getSize() , data.getCurrent()), null).getRecords());
    }

    @ApiOperation(value = "查询当前 UserOrder")
    @RequestMapping(value = "/id", method = RequestMethod.POST)
    public CommonResult<UserOrder> userOrderId(@RequestBody UserOrder data,@RequestHeader String token) {
        return CommonResult.data(mapper.selectById(data.getId()));
    }

    @ApiOperation(value = "增加 UserOrder")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public CommonResult<Integer> create(@RequestBody UserOrder data,@RequestHeader String token) {

        if (null==data.getBarberId() || null==data.getReservesId() || null==data.getTime()){
            return CommonResult.failed("字段不能为空");
        }

        Integer barberId = data.getBarberId();
        String reservesId =data.getReservesId();
        if (data.getTime() < System.currentTimeMillis() + (1000 * 60 * 60 * 4)){
            return CommonResult.failed("请提前4小时预定");
        }
        Integer userId = JwtHelper.getByUserId(token);
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<UserInfo>();
        wrapper.eq("user_id", userId); //请输入排异条件
        if (null != infoMapper.selectOne(wrapper)) {
            return CommonResult.failed("用户不能是设计师");
        }

        List<String> reservesIdList = Arrays.asList(reservesId.split(";;"));

        for(String id : reservesIdList){
            QueryWrapper<ReserveCategory> reserveCategoryWrapper = new QueryWrapper<ReserveCategory>();
            reserveCategoryWrapper.eq("id", Integer.parseInt(id)); //请输入排异条件
            if (null == reserveCategoryMapper.selectOne(reserveCategoryWrapper)) {
                return CommonResult.failed("没有当前类型");
            }
        }

        UserInfo info = infoMapper.selectById(barberId);
        if (null == info) {
            return CommonResult.failed("没有当前设计师");
        }

        data.setCreateTime(null);
        data.setUpdataTime(null);
        data.setUserId(userId);
        data.setType(1);
        Integer result = mapper.insert(data);
        if (result == 1){
            return CommonResult.data(result);
        }else {
            return CommonResult.failed("创建失败");
        }
    }

    @ApiOperation(value = "更新 UserOrder")
    @RequestMapping(value = "/upData", method = RequestMethod.POST)
    public CommonResult<Integer> upData(@RequestBody UserOrder data, @RequestHeader String token) {
        Integer id = data.getId();
        if (null==data.getId()){
            return CommonResult.failed("订单号不能为空");
        }
        UserOrder order = new UserOrder();
        order.setUserId(id);
        Integer result = mapper.updateById(order);
        if (result==1){
            return CommonResult.data(result);
        }else {
            return CommonResult.failed("更新失败");
        }
    }

    @ApiOperation(value = "删除 UserOrder")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public CommonResult<Integer> delete(@RequestBody UserOrder data,@RequestHeader String token) {
        Integer result = mapper.deleteById(data.getId());
        if (result==1){
            return CommonResult.data(result);
        }else {
            return CommonResult.failed("删除失败");
        }
    }
}